var t = getApp(), a = require("../../components/panzoom.js"), e = null;

Page({
    data: {
        home_btn: t.home_btn,
        showCart: t.showCart,
        showInfo: t.showInfo,
        showZhuanPei: t.showZhuanPei,
        server: t.rserver,
        serverUrl: "http://www.ylhzzy.top:8828/public",
        prd: null,
        curr: 0,
        id: null,
        inited: !1,
        transform3d: "",
        lastMove: 0,
        usr: null,
        hideCart: !1,
        showQRShare: t.showQRShare,
        num: 0,
        watermark: "",
        close_cut: ""
    },
    onLoad: function(r) {
        console.log("options", r), t.fromOptions(r), this.setData({
            options: r,
            route: this.route
        }), e = new a(this), this.loadPrd(r.id), this.reload();
        var o = wx.getStorageSync("watermark");
        this.setData({
            watermark: o
        }), console.log("watermark", o);
        var i = wx.getStorageSync("close_cut");
        this.setData({
            close_cut: i
        });
    },
    reload: function() {
        var t = this;
        wx.getStorage({
            key: "usr",
            complete: function(a) {
                t.setData({
                    usr: a.data
                }), t.refreshCartNum();
            }
        });
    },
    loadPrd: function(a) 
    {
        var r = this;
        t.request({
            url: t.rserver + "/productApi/getProductInfo",
            method: "POST",
            data: {
                id: a
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) 
            {
                r.setData({
                    inited: !0,
                    id: a,
                    curr: 0,
                    prd: t.data.result.product
                });
                wx.setNavigationBarTitle({
                    title: t.data.result.product.title
                });
                e.reset();
            }
        });
    },
    toVideo: function(t) {
        var a = t.currentTarget.dataset.url;
        wx.navigateTo({
            url: "/pages/video/play/play?url=" + encodeURIComponent(a)
        });
    },
    next: function(t) {
        var a = t.currentTarget.dataset.id;
        this.loadPrd(a);
    },
    switchColor: function(t) {
        t.currentTarget.dataset.index !== this.data.curr && this.setData({
            hideCart: !1,
            curr: t.currentTarget.dataset.index
        });
    },
    toInfo: function() {
        wx.navigateTo({
            url: "/pages/products/detailinfo?id=" + this.data.id
        });
    },
    toPlay: function() {
        wx.navigateTo({
            url: "/pages/play/play?id=" + this.data.id
        });
    },
    toSplit: function() {
        var t = JSON.stringify(this.data.prd.attr[this.data.curr]);
        wx.navigateTo({
            url: "/pages/products/split/split?attr=" + t
        });
    },
    toCart: function() {
        this.data.usr ? wx.navigateTo({
            url: "/pages/order/cart/cart"
        }) : wx.navigateTo({
            url: "/pages/usr/login/login"
        });
    },
    addToCart: function(a) {
        var e = this;
        if (this.data.usr) {
            var r = a.currentTarget.dataset.index, o = this.data.prd.attr[r];
            t.request({
                url: t.server + "/orderApi/addUserCart",
                method: "POST",
                data: {
                    userToken: this.data.usr.userToken,
                    pc_id: o.id,
                    quality: 1
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: function(t) {
                    1e3 !== t.data.code ? (wx.showToast({
                        icon: "none",
                        title: t.data.msg.zh
                    }), e.refreshCartNum()) : wx.navigateTo({
                        url: "/pages/usr/login/login"
                    });
                }
            });
        } else wx.navigateTo({
            url: "/pages/usr/login/login"
        });
    },
    refreshCartNum: function() {
        var a = this;
        this.data.usr ? t.request({
            url: t.server + "/orderApi/getUserCart",
            method: "POST",
            data: {
                token: t.token,
                userToken: this.data.usr.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var e = 0;
                0 === t.data.code && (e = t.data.result.products.length), a.setData({
                    num: e
                });
            }
        }) : this.setData({
            num: 0
        });
    },
    touchstart: function(t) {
        e.touchstart(t);
    },
    touchmove: function(t) {
        e.touchmove(t), this.data.lastMove = new Date().getTime();
    },
    touchend: function(t) {
        e.touchend(t);
    },
    hideTouchStart: function(t) {
        t.currentTarget.dataset.index === this.data.curr && (this.data.startPoint = t.touches[0], 
        this.data.endPoint = null, this.data.startTime = new Date().getTime());
    },
    hideTouchMove: function(t) {
        t.currentTarget.dataset.index === this.data.curr && (this.data.endPoint = t.touches[0]);
    },
    hideTouchEnd: function(t) {
        if (t.currentTarget.dataset.index === this.data.curr) {
            var a = this.data.startTime, e = new Date().getTime(), r = this.data.startPoint, o = this.data.endPoint;
            if (o) {
                var i = Math.max(Math.abs(o.clientX - r.clientX), Math.abs(o.clientY - r.clientY)), n = i / (e - a);
                i > 20 && n > .08 && this.setData({
                    hideCart: !this.data.hideCart
                });
            }
        }
    },
    back: function() {
        new Date().getTime() - this.data.lastMove < 1e3 || wx.navigateBack();
    },
    onShow: function() {
        this.refreshCartNum();
    },
    onShareAppMessage: function() {}
});